#ifdef UTIL_PLATFORM_WIN32
#include "lang/Thread.h"
#include <cstddef>
#include "windows.h"
#include "lang/Exception.h"
using namespace yzrilyzr_lang;
namespace yzrilyzr_lang{
	void Thread::setPriority(Priority p){
		int np=THREAD_PRIORITY_NORMAL;
		if(p == LOW)np=THREAD_PRIORITY_LOWEST;
		else if(p == NORMAL)np=THREAD_PRIORITY_NORMAL;
		else if(p == HIGH)np=THREAD_PRIORITY_HIGHEST;
		SetThreadPriority(mHandle, np);
	}
	void Thread::setName(const String & name){
		std::wstring ss=name.towstring();
		SetThreadDescription(mHandle, ss.c_str());
	}
	std::shared_ptr<Thread> Thread::currentThread(){
		return std::make_shared<Thread>(GetCurrentThread());
	}
	void Thread::join(){
		// 等待线程结束，INFINITE表示无限等待
		DWORD result=WaitForSingleObject(mHandle, INFINITE);
		if(result != WAIT_OBJECT_0){
			// 处理错误，例如句柄无效
			throw RuntimeException("Failed to join thread");
		}
		// 关闭线程句柄释放资源
		CloseHandle(mHandle);
		// 重置句柄，避免重复join
		mHandle=NULL;
	}
}
#endif